package cn.agiledata.bank.myspace.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import cn.agiledata.bank.common.dao.AccountTemplate;
import cn.agiledata.bank.common.dao.BankTemplate;
import cn.agiledata.bank.common.dao.SysTemplate;
import cn.agiledata.bank.common.dao.TransactionTemplate;
import cn.agiledata.bank.common.util.Constant;
import cn.agiledata.bank.common.util.OperateUtil;
import cn.agiledata.bank.common.util.StringUtil;
import cn.agiledata.bank.common.util.WeatherUtil;
import cn.agiledata.bank.hibernateBean.Account;
import cn.agiledata.bank.hibernateBean.AccountBank;
import cn.agiledata.bank.hibernateBean.BankInfo;
import cn.agiledata.bank.hibernateBean.SubAccount;
import cn.agiledata.bank.hibernateBean.SysParameter;
import cn.agiledata.bank.hibernateBean.Transaction;
import cn.agiledata.bank.myspace.bean.AccountHisQueryDetailBean;
import cn.agiledata.bank.myspace.bean.PrintTransactionHisBean;
import cn.agiledata.bank.myspace.bean.PrintTransactionHisQueryBean;
import cn.agiledata.bank.myspace.dao.PrintTransactionHisDao;
import com.vnv.infrastructure.logic.IPBAppFactory;

public class PrintTransactionHisServiceImp implements
        PrintTransactionHisService {
    PrintTransactionHisDao printTransactionHisDao;

    TransactionTemplate transaction;

    AccountTemplate accounttemple;

    private SysTemplate getSysTemplate() {
        SysTemplate sysTemplate = (SysTemplate) IPBAppFactory
                .getObj("sysTemplate");
        return sysTemplate;
    }

    public void setTransaction(TransactionTemplate transaction) {
        this.transaction = transaction;
    }

    public void setPrintTransactionHisDao(
            PrintTransactionHisDao printTransactionHisDao) {
        this.printTransactionHisDao = printTransactionHisDao;
    }

    public void setAccounttemple(AccountTemplate accounttemple) {
        this.accounttemple = accounttemple;
    }

    public BankTemplate getBankTemplate() {
		BankTemplate bt = (BankTemplate)IPBAppFactory.getObj("bankTemplate");
		return bt;
	}
    
    public List getTransactionHisBydatePageTotal(Map map) {

        List list = printTransactionHisDao
                .getTransactionHisBydatePageTotal(map);
        List pageList = new ArrayList();

        for (int i = 0; i < list.size(); i++) {

            PrintTransactionHisQueryBean printTransHis = (PrintTransactionHisQueryBean) list
                    .get(i);
            int pageCount = printTransHis.getPageCount();
          
            for (int ii = 0; ii < pageCount; ii++) {
                PrintTransactionHisQueryBean printQuery = new PrintTransactionHisQueryBean();
                printQuery.setSubAccountId(printTransHis.getSubAccountId());
                printQuery.setPageCount(printTransHis.getPageCount());
                printQuery.setNowPage((ii + 1));
               
                pageList.add(printQuery);
            }

        }

        return pageList;
    }

    /**
     * 得到每页要打印的数据
     * 
     * @param map
     * @return
     */
    public List getTransactionHisByDatePageList(Map map) {
        List newList = new ArrayList();
        List list = printTransactionHisDao.getTransactionHisByDatePageList(map);
        String subAccountId = (String) map.get("subAccountID");
        String account = "";
        for (int i = 0; i < list.size(); i++) {
            PrintTransactionHisBean printBean = (PrintTransactionHisBean) list
                    .get(i);
            String realAmount = getRealTrasAmount(printBean, subAccountId);
//            if (new Long(printBean.getCreditSeq()).toString().equals(
//                    subAccountId)) {
//                printBean.setGathering(realAmount);
//            } else if (new Long(printBean.getDebitSeq()).toString().equals(
//                    subAccountId)) {
//                printBean.setPayment(realAmount);
//            }
            double amount = printBean.getAmount();
            double flag = Double.parseDouble(printBean.getFlag());
            if (amount * flag > 0) {
            	printBean.setGathering(realAmount.replaceAll("-", ""));
            } else {
            	printBean.setPayment(realAmount.replaceAll("-", ""));
            }
            account = getOtherAccountInfo(subAccountId, account, printBean);
            newList.add(printBean);
        }
        return newList;
    }

    /**
     * 得到对方帐户信息
     * 
     * @param subAccountId
     * @param account
     * @param printBean
     * @return
     */
    private String getOtherAccountInfo(String subAccountId, String account,
            PrintTransactionHisBean printBean) {
    	Transaction tt = transaction.getTransactionByTranId(printBean
                .getTransId());
    	
    	//账户间交易，子账户转账
        if (String.valueOf(printBean.getActionSeq()).equals(
                Constant.ACTION_TYPE_C) || String.valueOf(printBean.getActionSeq()).equals(
                        Constant.ACTION_TYPE_H)) {// 根据printBean.getDebitSeq()或者printBean.getCreditSeq()取出Account中的email，name
            
            if (tt.getCredit().getId().toString().equals(subAccountId)) {// 收款方
                if (StringUtil.isStrEmpty(tt.getDebit().getAccount().getEmail())) {
                    account = tt.getDebit().getAccount().getMobile();
                } else {
                    account = tt.getDebit().getAccount().getEmail();
                }
                if (String.valueOf(printBean.getActionSeq()).equals(
                        Constant.ACTION_TYPE_H)) {//子账户转账
                	account += "(" + Constant.SUBACCOUNT_TITLE + tt.getDebit().getAccountName() + ")";
                }
                printBean.setEmail(account);
                printBean.setName(tt.getDebit().getAccount().getName());
            } else {
                if (StringUtil.isStrEmpty(tt.getCredit().getAccount().getEmail())) {
                    account = tt.getCredit().getAccount().getMobile();
                } else {
                    account = tt.getCredit().getAccount().getEmail();
                }
                if (String.valueOf(printBean.getActionSeq()).equals(
                        Constant.ACTION_TYPE_H)) {//子账户转账
                	account += "(" + Constant.SUBACCOUNT_TITLE + tt.getCredit().getAccountName() + ")";
                }
                printBean.setEmail(account);
                printBean.setName(tt.getCredit().getAccount().getName());
            }
        }
        
        //提现
        if (String.valueOf(printBean.getActionSeq()).equals(Constant.ACTION_TYPE_D)) {
        	BankInfo bi = (BankInfo)getBankTemplate().getBankInfo(tt.getBankSeq().toString());
        	if (StringUtil.isStrEmpty(tt.getCardNo())) {
    			printBean.setEmail(bi.getBankName());
    		} else {
    			printBean.setEmail("银行卡号:"+tt.getCardNo()+"("+bi.getBankName()+")");
    		}
    		printBean.setName(tt.getPersonName());
        }
        
        //账户付款给对方银行
        if (String.valueOf(printBean.getActionSeq()).equals(Constant.ACTION_TYPE_G)) {// 账户付款给对方银行,account_bank_seq关联account_bank的id，取出account_bank中的account_id,account_name
            BankInfo bi = (BankInfo)getBankTemplate().getBankInfo(tt.getBankSeq().toString());
            if (StringUtil.isStrEmpty(tt.getCardNo())) {
        		printBean.setEmail(bi.getBankName());
        	} else {
        		printBean.setEmail("银行卡号:"+tt.getCardNo()+"("+bi.getBankName()+")");
        	}
        	printBean.setName(tt.getPersonName());
            
        }
        
        //网上银行充值
    	if (String.valueOf(printBean.getActionSeq()).equals(Constant.ACTION_TYPE_A)
    			|| String.valueOf(printBean.getActionSeq()).equals(Constant.ACTION_TYPE_F)) {
    		BankInfo bi = (BankInfo)getBankTemplate().getBankInfo(tt.getBankSeq().toString());
    		if (StringUtil.isStrEmpty(tt.getCardNo())) {
    			printBean.setEmail(bi.getBankName());
    		} else {
    			printBean.setEmail("银行卡号:"+tt.getCardNo()+"("+bi.getBankName()+")");
    		}
    		printBean.setName(tt.getPersonName());
    	}
        
        //线下充值
        if (String.valueOf(printBean.getActionSeq()).equals(
                Constant.ACTION_TYPE_B)) {// 线下冲值,printBean.getCreditSeq()取出Account中的email，name为收款方信息
        	if (tt.getBankSeq() != null && tt.getBankSeq().intValue() > 0) {
        		BankInfo bi = (BankInfo)getBankTemplate().getBankInfo(tt.getBankSeq().toString());
        		if (StringUtil.isStrEmpty(tt.getCardNo())) {
        			printBean.setEmail(bi.getBankName());
        		} else {
        			printBean.setEmail("银行卡号:"+tt.getCardNo()+"("+bi.getBankName()+")");
        		}
        		printBean.setName(tt.getPersonName());
        	} else {
        		printBean.setEmail("邮局");
        		printBean.setName(tt.getPersonName());
        	}
        }
        
        //卡到账户，交易对方为卡
        if (String.valueOf(printBean.getActionSeq()).equals(Constant.ACTION_TYPE_X)) {
        	if (printBean.getCardNO()==null || printBean.getCardNO().equals("")) {
        		printBean.setEmail("卡卡号不详");
        	} else {
        		printBean.setEmail("卡卡号:" + printBean.getCardNO());
        	}
        	
        	printBean.setName(tt.getDebit().getAccount().getName());
        }
        //账户到卡
        if (String.valueOf(printBean.getActionSeq()).equals(Constant.ACTION_TYPE_Y)) {
        	if (printBean.getCardNO()==null || printBean.getCardNO().equals("")) {
        		printBean.setEmail("卡卡号不详");
        	} else {
        		printBean.setEmail("卡卡号:" + printBean.getCardNO());
        	}
            printBean.setName(tt.getCredit().getAccount().getName());
        }
        
        //冲正
        if (String.valueOf(printBean.getActionSeq()).equals(Constant.ACTION_TYPE_E)) {
        	printBean.setEmail("");
    		printBean.setName("");
        }
        
        //调整
        if (String.valueOf(printBean.getActionSeq()).equals(Constant.ACTION_TYPE_I)) {
        	printBean.setEmail("");
    		printBean.setName("");
        }
        
        //利息
        if (String.valueOf(printBean.getActionSeq()).equals(Constant.ACTION_TYPE_J)) {
        	printBean.setEmail("");
    		printBean.setName("");
        }
        
        return account;
    }

    /**
     * 得到上月余额
     * 
     * @param map
     * @return
     */
    public PrintTransactionHisBean getLastMonthBalance(Map map) {
        PrintTransactionHisBean printBean = printTransactionHisDao
                .getLastMonthBalance(map);
        String subAccountId = (String) map.get("subAccountID");
        SubAccount subAccount = accounttemple.getSubAccount(subAccountId);
        printBean.setName(subAccount.getAccount().getName());
        if (subAccount.getAccount().getEmStatus().equals(Constant.ACCOUNT_STATE_NORMAL)) {
        	printBean.setEmail(subAccount.getAccount().getEmail());
        } else if (subAccount.getAccount().getMobStatus().equals(Constant.ACCOUNT_STATE_NORMAL)) {
        	printBean.setEmail(subAccount.getAccount().getMobile());
        }
        printBean.setSubAccountName(subAccount.getAccountName());
        return printBean;
    }

    /**
     * 得到实际金额
     * 
     * @param printBean
     * @param subAccountId
     * @return
     */
    private String getRealTrasAmount(PrintTransactionHisBean printBean,
            String subAccountId) {

        boolean add = false;
        if (String.valueOf(printBean.getActionSeq()).equals(
                Constant.ACTION_TYPE_C)) { // 如果是担保交易,付款方的手续费是外扣的
            if (new Long(printBean.getDebitSeq()).toString().equals(
                    subAccountId)) {

                add = true;
            }
        }

        if (String.valueOf(printBean.getActionSeq()).equals(
                Constant.ACTION_TYPE_D)
                || (String.valueOf(printBean.getActionSeq())
                        .equals(Constant.ACTION_TYPE_G))) { // 如果是提现或者账户付款给对方银行,取现时收费模式=1是外扣,=2是内扣

            SysParameter feeMode = getSysTemplate().getWithDrawFee(
                    new Long(printBean.getCreditSeq()));
            String withdraw_fee = feeMode.getParmValue();
            if (withdraw_fee.equals(Constant.WITHDRAW_FEE_FRIST)) {
                add = true;
            }
        }

        if (add) {
            return OperateUtil.parseDec(
                    new Double(printBean.getAmount()
                            + new Double(getFee(printBean, subAccountId))
                                    .doubleValue()), "######0.00");

        } else {
            return OperateUtil.parseDec(
                    new Double(printBean.getAmount()
                            - new Double(getFee(printBean, subAccountId))
                                    .doubleValue()), "######0.00");

        }
    }

    // 费用
    private String getFee(PrintTransactionHisBean printBean, String subAccountId) {
        if (new Long(printBean.getCreditSeq()).toString().equals(subAccountId))
            return printBean.getCreditFee() + "";
        else
            return printBean.getDebitFee() + "";
    }

}
